/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package lime2;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import sagroup.util.Lights;

import lights.interfaces.ITuple;

/**
 * This class now can not make junit green, because it is just a demo
 * implementation of RRMgr simply using concatenation of all fields' class name
 * as Hash key. Sure, it can make junit green if modified. But for now we don't
 * need it and leave it as abstract to avoid misuse. If you really complete it,
 * below are the things to do: support wild card.
 * 
 * @author Xing Jiankuan
 * 
 */
abstract class TypeNameHashRRMgr extends ABaseRRMgr {
	class Entry {
		List<ITuple> tuples = new LinkedList<ITuple>();
		Map<ReactionID, RegisteredReaction> rrs = new HashMap<ReactionID, RegisteredReaction>();
	}

	// all tuples to be put in ts and all reactions
	Map<String, Entry> trs = new LinkedHashMap<String, Entry>();

	protected void putReaction(RegisteredReaction rr) {
		String typeName = Lights.tupleAllFieldTypeStr(rr.getTemplate());
		if (!trs.containsKey(typeName)) {
			trs.put(typeName, new Entry());
		}
		trs.get(typeName).rrs.put(rr.getReactionID(), rr);
	}

	private void put(ITuple t) {
		String typeName = Lights.tupleAllFieldTypeStr(t);
		if (!trs.containsKey(typeName)) {
			trs.put(typeName, new Entry());
		}
		trs.get(typeName).tuples.add(t);
	}

	TypeNameHashRRMgr(Hashtable<ReactionID, RegisteredReaction> reg) {
		super(reg);
	}

	protected void remove(RegisteredReaction rr) {
		String typeName = Lights.tupleAllFieldTypeStr(rr.getTemplate());
		if (trs.containsKey(typeName)) {
			trs.get(typeName).rrs.remove(rr.getReactionID());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lime2.IRRMgr#findMatchTemplate(lights.interfaces.ITuple)
	 */
	public List<RegisteredReaction> findMatchTemplate(ITuple newTuple) {
		List<RegisteredReaction> ret = new LinkedList<RegisteredReaction>();
		Entry e = trs.get(Lights.tupleAllFieldTypeStr(newTuple));
		if (e == null)
			return ret;
		Map<ReactionID, RegisteredReaction> map = e.rrs;
		ABaseRRMgr.findMatchTemplate(map, newTuple, ret);
		return ret;
	}

}

class Entry {
	List<ITuple> tuples = new LinkedList<ITuple>();
	Map<ReactionID, RegisteredReaction> rrs = new HashMap<ReactionID, RegisteredReaction>();
}
